/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.service;

import org.apache.log4j.Logger;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Date of create: Oct 7, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.1007
 */
public class SearchQueryImpl implements SearchQuery {
    
    private Logger log = Logger.getLogger(SearchQueryImpl.class);
    private List<String> fullQuery = new ArrayList<String>();
    private List<String> baseQuery = new ArrayList<String>();
    
    
    public void addWord(String word) {
        baseQuery.add(word.trim());
    }

    public void addExactWord(String word) {
        fullQuery.add("+" + word.trim());
    }

    public void addExactPhrase(String phrase) {
        fullQuery.add("\"" + phrase.trim() + "\"");
    }

    public void addChoices(String... choices) {
        String param = "";
        for (String choice : choices) {
            param += " OR " + choice.trim();
        }
        fullQuery.add(param.substring(4));
    }

    public void addSynonym(String synonym) {
        fullQuery.add("~" + synonym.trim());
    }

    public void addSearchInSite(URL site) {
        fullQuery.add("site:" + site.getHost().trim());
    }

    public void addSearchInTitle(String inTitleString) {
        fullQuery.add("intitle:" + inTitleString.trim());
    }

    public void addSearchInUrl(String inUrlString) {
        fullQuery.add("inurl:" + inUrlString.trim());
    }

    public void addFiletype(String extension) {
        fullQuery.add("filetype:" + extension.trim());
    }

    public void addExcludeWord(String word) {
        fullQuery.add("-" + word.trim());
    }

    public void addExcludeSearchInSite(URL site) {
        fullQuery.add("-site:" + site.getHost().trim());
    }

    public String getBaseQuery() {
        String result = "";
        for (Iterator<String> it = baseQuery.iterator(); it.hasNext();) {
            String string = it.next();
            result += " " + string;
        }
        return result;
    }

    public String getFullQuery() {
        String result = getBaseQuery();
        for (Iterator<String> it = fullQuery.iterator(); it.hasNext();) {
            String string = it.next();
            result += " " + string;
        }
        return result;
    }

    public void resetQuery() {
        fullQuery.clear();
        baseQuery.clear();
    }
    
}
